<?php
// +--------------------------------------------------------------------------
// | ISDCE [ 载攸道 先顺得常 ] <http://www.zaiyoudao.com>
// +--------------------------------------------------------------------------
// | Copyright © 2009-2017 载攸道 版权所有 <TEL:086-010-57209502>
// +--------------------------------------------------------------------------
// | Project: YiPHP [ 我会的仅仅是偷懒！ ] <http://www.yiphp.com>
// +--------------------------------------------------------------------------
// | Author: 牛很多戒很多不戒 <n@isdce.com> <http://juexue.wang>
// +--------------------------------------------------------------------------

/**
 * [ 后台模块 ]
 */
namespace Admin\Controller;
use Common\Controller\AdminController;

/**
 * 行为设置管理
 */
class BehaviorController extends AdminController{
	
	/**
	 * 行为记录
	 */
    public function Log(){
		
		action_log('admin_behavior_log');//行为记录
		

		//页面配置
		$page = array(
			
			'title'=>'行为记录',
			'keywords'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'description'=>'YiPHP [ 我会的仅仅是偷懒！]',
			'author'=>'YiPHP',
			'robots' => 'all',
		
		);
		
		$this->assign('page',$page);
		
		//实例化数据库
		$m_common_user_behavior = M('common_user_behavior');
		$m_common_user = M('common_user');
		$m_common_user_behavior_log = M('common_user_behavior_log');
		
		//获得指定行为
		$bid = I('param.bid')?I('param.bid'):0;
		
		//获得行为列表
		$behavior = $m_common_user_behavior->where('status=1')->select();
		
		//判断哪个被选择了
		if($bid!=0){
			
			foreach($behavior as $bi=>$bk){
				
				if($behavior[$bi]['id']==$bid){
					
					$behavior[$bi]['selected'] = "selected";
				}
			}
			$map['bid'] =$bid;
		}
		
		//selected="selected"
		$this->assign('behavior',$behavior);
		$this->assign('behaviors',$behavior);
		
		
		//获得查询用户UID
		$uid = I('param.uid')?I('param.uid'):'';

		if($uid!=0){
			
			$map['uid'] =$uid;
		}
		
		//selected="selected"
		$this->assign('uid',$uid);
		
		//行为记录开始和结束时间
		$begin_time = strtotime(I('param.begin_time'));
		$end_time = strtotime(I('param.end_time'));
		if($begin_time != null and $end_time != null){
			
			$map['time']  = array(array('egt',$begin_time),array('elt',$end_time),'and'); 
			
		}else{
			//默认今天明天
			$begin_time=time();
			$end_time=time()+86400;
		}
		
		
		$this->assign('begin_time',$begin_time);
		$this->assign('end_time',$end_time);
		
		
		
		//默认第一页
		$_GET['p']=$_GET['p']?$_GET['p']:1;
		
		//分页查询
		$list = $m_common_user_behavior_log->where($map)->order('time desc')->page($_GET['p'].',20')->select();
		$count      = $m_common_user_behavior_log->where($map)->count();// 查询满足要求的总记录数
		$Page       = new \Think\Page($count,20);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		$this->assign('pages',$show);
		
		//获得关联信息
		foreach($list as $i=>$k){
			
			$bmap['id'] = $list[$i]['bid'];
			
			$bdata = $m_common_user_behavior->where($bmap)->find();
			
			$umap['id'] = $list[$i]['uid'];
			
			$udata = $m_common_user->where($umap)->find();
			
			$list[$i]['name'] = $bdata['name'];
			$list[$i]['nickname'] = $udata['nickname'];
			$list[$i]['img'] = $udata['img'];
			
			if(getUID()==1){
				
				$list[$i]['look'] = 1;
			
			}
			

		}
	
		
		$this->assign('list',$list);
		
		
		$this->display();
		
	}
	
	/**
	 * 行为管理
	 */
    public function Manage(){
		
		action_log('admin_behavior_manage');//行为记录
		//页面信息
		common_public()->assign_page('用户行为');
		
		//实例化数据库
		$m_common_user_behavior = M('common_user_behavior');
		
		//获得上级行为信息
		$fmap['id'] = I('param.fbid')?I('param.fbid'):0;
		$data = $m_common_user_behavior->where($fmap)->find();
		
		//获得同级行为信息
		$map['fbid'] = $fmap['id'];
		$map['status'] = 1;
		
		$list = $m_common_user_behavior->where($map)->order('sole asc')->select();
		
		//赋值模板
		$this->assign('tier',$data['tier']+1);
		$this->assign('list',$list);
		$this->assign('data',$data);
		$this->assign('fbid',$fmap['id']);
		
		$this->display();
		
	}
	
	/**
	 * 新增行为
	 */
	public function Create(){
		
		action_log('admin_behavior_create');//行为记录
		//判断是否是ajax请求
		if(IS_AJAX){

			$m_common_user_behavior = M('common_user_behavior');
		
			//获得数据
			$data = I();
			
			$data['status'] = 1;
			
			$result = $m_common_user_behavior->add($data);
			
			//返回信息
			$info = $result?get_errcode(0):get_errcode(-1);
				
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);
		
	}
	
	
	/**
	 * 编辑行为
	 */
	public function Edit(){
		
		action_log('admin_behavior_edit');//行为记录
		//判断是否是ajax请求
		if(IS_AJAX){

			$m_common_user_behavior = M('common_user_behavior');
		
			$map['id'] = I('param.id');
			
			$data = I('');
			//删除主键
			unset($data['id']);
		
			$result = $m_common_user_behavior->where($map)->save($data);
			
			//返回信息
			$info = $result?get_errcode(0):get_errcode(-1);
		
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);
		
	}
	
	/**
	 * 批量删除
	 */
	public function Delete($id,$status=0){
		
		action_log('admin_behavior_delete');//行为记录
		//判断是否是ajax请求
		if(IS_AJAX){
				
			$m_common_user_behavior = M('common_user_behavior');
		
			//批量ID
			$id = $id?$id:I('param.id');
			$status = $status?$status:I('param.status');
			
			if($id){
				
				//批量ID数组
				$id = explode(',',$id); 

				foreach($id as $i=>$k){
					
					$map['fbid'] = $id[$i];
					
					//删除同级即下级
					$m_common_user_behavior->delete($id[$i]);
					$result = $m_common_user_behavior->where($map)->delete();
					
				}
				
				//返回信息
				$info = get_errcode(0);
			
			}else{
				
				$info = get_errcode(10000001);
			}
			
		}
		
		//返回信息		
		$info = $info?$info:get_errcode(-2);

		$this->ajaxReturn($info);

	}
	
}